package com.ruoyi.report_wjjhl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DSTransactional;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.report_wjjhl.domain.bo.CjlkljpsyBaDetailBo;
import com.ruoyi.report_wjjhl.domain.vo.CjlkljpsyBaDetailVo;
import com.ruoyi.report_wjjhl.domain.CjlkljpsyBaDetail;
import com.ruoyi.report_wjjhl.mapper.CjlkljpsyBaDetailMapper;
import com.ruoyi.report_wjjhl.service.ICjlkljpsyBaDetailService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 粗集料详细Service业务层处理
 *
 * @author zyb
 * @date 2024-08-08
 */
@RequiredArgsConstructor
@Service
@DSTransactional
public class CjlkljpsyBaDetailServiceImpl implements ICjlkljpsyBaDetailService {

    private final CjlkljpsyBaDetailMapper baseMapper;

    /**
     * 查询粗集料详细
     */
    @Override
    public CjlkljpsyBaDetailVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询粗集料详细列表
     */
    @Override
    public TableDataInfo<CjlkljpsyBaDetailVo> queryPageList(CjlkljpsyBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CjlkljpsyBaDetail> lqw = buildQueryWrapper(bo);
        Page<CjlkljpsyBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询粗集料详细列表
     */
    @Override
    public List<CjlkljpsyBaDetailVo> queryList(CjlkljpsyBaDetailBo bo) {
        LambdaQueryWrapper<CjlkljpsyBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CjlkljpsyBaDetail> buildQueryWrapper(CjlkljpsyBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CjlkljpsyBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getExcelId() != null, CjlkljpsyBaDetail::getExcelId, bo.getExcelId());
        lqw.eq(bo.getShaikcc() != null, CjlkljpsyBaDetail::getShaikcc, bo.getShaikcc());
        lqw.eq(bo.getShaisz1() != null, CjlkljpsyBaDetail::getShaisz1, bo.getShaisz1());
        lqw.eq(bo.getFenjsy1() != null, CjlkljpsyBaDetail::getFenjsy1, bo.getFenjsy1());
        lqw.eq(bo.getLeijsy1() != null, CjlkljpsyBaDetail::getLeijsy1, bo.getLeijsy1());
//        lqw.eq(bo.getTonggbfl() != null, CjlkljpsyBaDetail::getTonggbfl, bo.getTonggbfl());
        lqw.eq(bo.getShaisz2() != null, CjlkljpsyBaDetail::getShaisz2, bo.getShaisz2());
        lqw.eq(bo.getFenjsy2() != null, CjlkljpsyBaDetail::getFenjsy2, bo.getFenjsy2());
        lqw.eq(bo.getLeijsy2() != null, CjlkljpsyBaDetail::getLeijsy2, bo.getLeijsy2());
//        lqw.eq(bo.getTonggbf2() != null, CjlkljpsyBaDetail::getTonggbf2, bo.getTonggbf2());
//        lqw.eq(bo.getTonggbf() != null, CjlkljpsyBaDetail::getTonggbf, bo.getTonggbf());
        lqw.eq(bo.getLeijsy() != null, CjlkljpsyBaDetail::getLeijsy, bo.getLeijsy());
        lqw.eq(bo.getZuidz() != null, CjlkljpsyBaDetail::getZuidz, bo.getZuidz());
        lqw.eq(bo.getZuixz() != null, CjlkljpsyBaDetail::getZuixz, bo.getZuixz());
        return lqw;
    }

    /**
     * 新增粗集料详细
     */
    @Override
    public Boolean insertByBo(CjlkljpsyBaDetailBo bo) {
        CjlkljpsyBaDetail add = BeanUtil.toBean(bo, CjlkljpsyBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改粗集料详细
     */
    @Override
    public Boolean updateByBo(CjlkljpsyBaDetailBo bo) {
        CjlkljpsyBaDetail update = BeanUtil.toBean(bo, CjlkljpsyBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CjlkljpsyBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除粗集料详细
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
